﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.Tools;
using System.Windows.Data;
using bdnt_pets_system.DataServiceReference;
using bdnt_pets_system_Entity;

namespace bdnt_pets_system.PageViews.TeamManager
{
    public partial class NewTeamControl : UserControl
    {
        DataServiceReference.DataServiceClient datasc;
        public NewTeamControl()
        {
            InitializeComponent();
            datasc = new DataServiceReference.DataServiceClient();
            tt = new Tbl_Team();
            InitEvnet();
            BingData();
        }

        void BingData()
        {
            tbNo.SetBinding(TextBox.TextProperty, new Binding("TeamNo")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbName.SetBinding(TextBox.TextProperty, new Binding("TeamName")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbType.SetBinding(TextBox.TextProperty, new Binding("TeamType")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbMNo.SetBinding(TextBox.TextProperty, new Binding("DeptNo")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbR.SetBinding(TextBox.TextProperty, new Binding("Remark") { Mode = BindingMode.TwoWay });
        }

        public Tbl_Team tt
        {
            get { return this.DataContext as Tbl_Team; }
            set { this.DataContext = value; }
        }

        public NewTeamControl(Tbl_Team cuTt)
        {
            InitializeComponent();
            datasc = new DataServiceReference.DataServiceClient();
            InitEvnet();
            BingData();
            tt = cuTt;
        }

        void InitEvnet()
        {
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            btnOk.Click += new RoutedEventHandler(btnOk_Click);
            datasc.SaveTeamCompleted += new EventHandler<SaveTeamCompletedEventArgs>(datasc_SaveTeamCompleted);
            datasc.GetTeamCompleted += new EventHandler<GetTeamCompletedEventArgs>(datasc_GetTeamCompleted);
        }

        void datasc_GetTeamCompleted(object sender, GetTeamCompletedEventArgs e)
        {
            tt = e.Result;
            BingData();
        }

        void datasc_SaveTeamCompleted(object sender, SaveTeamCompletedEventArgs e)
        {
            if (e.Result)
            {
                MessageBox.Show("保存成功！", "系统提示", MessageBoxButton.OK);
                BingData();
                App.ToPage(new TeamManagerControl());
            }
            else
            {
                MessageBox.Show("保存失败！", "系统提示", MessageBoxButton.OK);
                return;
            }
        }

        void btnOk_Click(object sender, RoutedEventArgs e)
        {
            tbNo.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tbName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tbType.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tbMNo.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            if (vs.Errors.Count() > 0)
            {
                errorMsg.Text = "数据填写不全！";
                return;
            }
            datasc.SaveTeamAsync(tt);
        } 

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            App.ToPage(new TeamManagerControl());
        }
    }
}
